<?php
namespace api\modules\v1\controllers;

use Yii;
use api\common\controllers\AController;
use api\common\tools\WxPayApi;
use api\common\tools\WechatPay;
use api\common\tools\WxPayUnifiedOrder;
use api\common\tools\WxPayConfig;
use common\records\Chargement;
use common\records\ChargeRule;
use common\records\UserInfo;
use yii\data\ActiveDataProvider;

class OtherpayController extends AController
{
    public $modelClass = 'common\records\Chargement';
    /**
     * @api {get} /v1/pays 记录列表
     * @apiVersion 1.0.0
     * @apiName index
     * @apiGroup records
     *
     * @apiParam {String} uid 用户id
     *
     */
    public function actionIndex()
    {
        if(!array_key_exists("uid",$_GET) || !array_key_exists("out_trade_no",$_GET))
        {
            $result = array();
            $detail = array();

            $detail['code'] = 400;
            $detail['reason'] = "fields missing";
            $result['result'] = $detail;
            return $result;
        }
        $uid = trim($_GET['uid']);
        $out_trade_no = trim($_GET['out_trade_no']);
        $chargement = Chargement::find()->where([ 'uid'=>$uid, 'out_trade_no'=>$out_trade_no])->one();
        if($chargement == null)
        {
            $result = array();
            $detail = array();
            
            $detail['code'] = 404;
            $detail['reason'] = "不存在的支付订单";
            $result['result'] = $detail;
            return $result;
        }
        $result = array();
        $detail = array();

        $detail['code'] = 200;
        $detail['reason'] = "获取支付信息成功";
        $detail['status'] = $chargement->status;
        $result['result'] = $detail;
        return $result;

    }

    /*
     @param charge_rule_id 订单id
     @param uid      用户ID
     */
    public function actionCreate()
    {
        $chargement = new $this->modelClass(); 
        if(!array_key_exists("money",$_POST) || !array_key_exists("uid",$_POST) || !array_key_exists("coin",$_POST))
        {
            $result = array();
            $detail = array();

            $detail['code'] = 400;
            $detail['reason'] = "字段缺失";
            $result['result'] = $detail;
            return $result;

        }
        $money = floatval(trim($_POST['money']));
        $coin = intval(trim($_POST['coin']));;
        $uid = trim($_POST['uid']);
        
        $user = UserInfo::find()->where(['uid'=>$uid])->one();
        if($user== null)
        {
            $result = array();
            $detail = array();

            $detail['code'] = 400;
            $detail['reason'] = "不存在的用户";
            $result['result'] = $detail;
            return $result;
        }

        
        $chargement->uid = $uid;
        $chargement->rule_id= -1;
        $chargement->money = $money;
        $chargement->coin = $coin;
        $chargement->create_time = date('Y-m-d H:i:s',time());
        $chargement->status= 0;
        $chargement->pay_type = 2;

        $out_trade_no = WxPayConfig::MCHID.date("YmdHis");
        $goods_tag = "coin";
        $total_fee = $money*100;

        $appid = Yii::$app->params['paygzh']['APPID'];
        $secret = trim(Yii::$app->params['paygzh']['APPSECRET']);
        $mch_id = trim(Yii::$app->params['paygzh']['MCHID']);
        $key = trim(Yii::$app->params['paygzh']['KEY']);
        $openid = $user->openidgzh;
        $notify_url = Yii::$app->params['paygzh']['notify_url'];
        
        $wxpay = new WechatPay($appid,$openid ,$mch_id,$key,$out_trade_no,$total_fee,$goods_tag,$notify_url);
        $res = $wxpay->pay();
        $result = array();
        $detail = array();
        //if($order['result_code'] != 'SUCCESS')
        //{
        //    $detail['code'] = 400;
        //    $detail['reason'] = "发起支付失败";
        //    $result['result'] = $detail;
        //}else{
            $chargement->out_trade_no = $out_trade_no;
            //$chargement->prepay_id = $res['prepay_id'];
            $chargement->save();
            $detail['code'] = 200;
            $detail['reason'] = "发起支付成功";
            $detail['package']= $res['package'];
            $detail['signType']= $res['signType'];
            $detail['nonceStr']= $res["nonceStr"];
            $detail['paySign']= $res["paySign"];
            $detail['out_trade_no'] = $out_trade_no;
            $detail['timeStamp'] = $res["timeStamp"];
            $result['result'] = $detail;
        //}

        return $result;

    }
}
